package com.ateam.biz.impl;

import java.util.List;

import com.ateam.biz.CompaniesBiz;
import com.ateam.dao.CompaniesDao;
import com.ateam.dao.UsersDao;
import com.ateam.pojo.Companies;
import com.ateam.pojo.Users;

/** 
* @author WuYuan
* 
*/
public class CompaniesBizImpl implements CompaniesBiz {
	private CompaniesDao companiesDao;
	
	private UsersDao usersDao;
	
	public CompaniesDao getCompaniesDao() {
		return companiesDao;
	}

	public void setCompaniesDao(CompaniesDao companiesDao) {
		this.companiesDao = companiesDao;
	}

	
	public UsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	@Override
	public int addCompany(Companies company, Users user) {
		// TODO Auto-generated method stub
		try {
			companiesDao.addCompany(company);
			user.setCompanyid(company.getCompanyid());
			user.setRole("1");
			System.out.println("addcompany中的user是"+user.toString());
			usersDao.updateUser(user);
			return companiesDao.updateUserCompany(user.getCompanyid(), user.getUserid(), 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public Companies getCompanyByUser(Users u) {
		try {
			return companiesDao.getCompanyByUserid(u.getUserid());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}return null;
	}

	@Override
	public List<Companies> getAllCompanies() {
		try {
			return companiesDao.getAllCompanies();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int updateApplyCompany(int companyid, int userid) {
		try {
			return companiesDao.updateUserCompany(companyid, userid, 0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int updatePassApply(int companyid, int userid) {
		try {
			Users user = usersDao.getUserByUserid(userid);
			user.setCompanyid(companyid);
			user.setRole("2");
			System.out.println("updatePassApply中的user是"+user.toString());
			usersDao.updateUser(user);
			return companiesDao.updateUserCompany(companyid, userid, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}
